

import { formatDateArray } from '/js/util.js'
let year = new Date().getFullYear(),
    month = new Date().getMonth() + 1,
    dates = new Date(year, month, 0).getDate(),
    weekdays = [],
    firstday = new Date(`${year}-${month}-1`).getDay() === 0 ? 7 : new Date(`${year}-${month}-1`).getDay(), //weekday
    lastMonthDays = firstday - 1,
    lastMonthDates = new Date(year, month - 1, 0).getDate(), //last month date
    nextMonthDays = 42 - lastMonthDays - dates,
    lastMonthDaysArray = [],
    currentYears = [],
    currentMonthDaysArray = [],
    currentMonths = [],
    nextMonthDaysArray = [],
    is_edit = false,
    is_click = false,
    click = [] //选择的时间

let getTeacherCourse = async (createtime, endtime) => {
    let res = await ajax('calendar_select', {
        createtime,
        endtime,
    }, 'post')
    let data = {
        1: {},
        2: {},
        3: {},
        4: {},
        5: {},
        6: {},
        7: {}
    }
    for (let i = 1; i < 8; i++) {
        for (let j = 0; j < 24; j++) {
            data[i][`${j < 10 ? '0' + j : j}:00:00`] = {}
        }
    }
    

    res?.data[1]?.forEach(item => {
        data[item.coursetime][item.timequantum] = item
    })

    res?.data[0]?.forEach(item => {
        data[item.coursetime][item.timequantum] = item
    })
    console.log('data',data)
    return data
}

export async function createCalendar() {
    return new Promise(async (resolve) => {

        for (var i = 0; i < 7; i++) {
            weekdays.push(new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * (i - (new Date().getDay() + 6) % 7)).getDate());
            currentYears.push(new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * (i - (new Date().getDay() + 6) % 7)).getFullYear())
            let month1 = new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * (i - (new Date().getDay() + 6) % 7)).getMonth() + 1
            currentMonths.push(month1)
            // weekdays.push(new Date().getTime() + 24 * 60 * 60 * 1000 * (i - (new Date().getDay() + 6) % 7).getDate());
        }

        let params = {
            'cuday': 1,
            'token': localStorage.getItem('Token')
        }

        let res = await getTeacherCourse(`${currentYears[0]}-${currentMonths[0] < 10 ? '0' + currentMonths[0] : currentMonths[0]}-${weekdays[0] < 10 ? '0' + weekdays[0] : weekdays[0]} 00:00:00`, `${currentYears[6]}-${currentMonths[6] < 10 ? '0' + currentMonths[6] : currentMonths[6]}-${weekdays[6] < 10 ? '0' + weekdays[6] : weekdays[6]} 00:00:00`)
        // let res = await ajax('student_lecture_time_record', params, 'post')

        // console.log('res', res)
        let calendarWrap = document.createElement('div')
        calendarWrap.className = 'calendar-wrap'
        calendarWrap.appendChild(createCalendarLeft())
        calendarWrap.appendChild(createCalendarRight())
        $('.body')[0].appendChild(calendarWrap)
        createDate({
            year,
            month
        })
        lastMonthAllow()
        nextMonthAllow()
        createCalendarRightWeekDay1(res[1], weekdays[0], currentMonths[0], currentYears[0])
        createCalendarRightWeekDay2(res[2], weekdays[1], currentMonths[1], currentYears[1])
        createCalendarRightWeekDay3(res[3], weekdays[2], currentMonths[2], currentYears[2])
        createCalendarRightWeekDay4(res[4], weekdays[3], currentMonths[3], currentYears[3])
        createCalendarRightWeekDay5(res[5], weekdays[4], currentMonths[4], currentYears[4])
        createCalendarRightWeekDay6(res[6], weekdays[5], currentMonths[5], currentYears[5])
        createCalendarRightWeekDay7(res[7], weekdays[6], currentMonths[6], currentYears[6])
        planNewClassListener()
        searchStudentListener()
        setTimeout(() => {
            $('.calendar-wrap')[0].style.opacity = 1
            resolve('ok')
        })

    })
}

let createCalendarLeft = () => {
    let left = document.createElement('div')
    /**
     * <div class='add-new-class' style='border: 1px solid #C9C9C9;color:#8C8C8C'>添加休息时间</div>
     *  <div style='display:flex;align-items:center;width:100%'>
            <div style='width: 13px;height: 13px;background: #D7D7A0;border-radius: 50%;margin-right:22px'></div>
            <div class='aside-calendar-blur'>第一节课</div>
        </div>
     * <div style='display:flex;align-items:center;width:100%;margin-top:12px'>
            <div style='width: 13px;height: 13px;background: #DE8386;border-radius: 50%;margin-right:22px'></div>
            <div class='aside-calendar-blur'>每周课</div>
        </div>
        <div style='display:flex;align-items:center;width:100%;margin-top:12px'>
            <div style='width: 13px;height: 13px;background: #C3C3C3;border-radius: 50%;margin-right:22px'></div>
            <div class='aside-calendar-blur'>没时间</div>
        </div>
     */
    left.className = 'calendar-wrap-left'
    left.innerHTML = `
        <div class='calendar-wrap-left-top'>
            <div class='plan-new-class' style='background: #4580FE;border: 1px solid white;color:white'>安排新课</div>
            <div class='set-new-class' style='border: 1px solid #C9C9C9;color:#8C8C8C'>设置可上课时间段</div>
            <div class='add-new-class' style='border: 1px solid #C9C9C9;color:#8C8C8C'>添加休息时间</div>
        </div>
        <div class='calendar-wrap-left-middle'>
            <div class='calendar-wrap-left-list'>
                <i class='iconfont icon-sousuo' style='color:#C1C1C1;font-size:16px;margin-left:9px;margin-right:7px'></i>
                <input class='search-student' placeholder='搜索学生' />
            </div>
            <div class='calendar-wrap-left-list2'>
                <div>${new Date().getFullYear()}年${new Date().getMonth() + 1}月</div>
                <div class='calendar-wrap-left-list2-allow'>
                    <div class='last-allow'><</div>
                    <div class='right-allow'>></div>
                </div>
            </div>
            <div class='calendar-wrap-left-list3'>
                <div>一</div>
                <div>二</div>
                <div>三</div>
                <div>四</div>
                <div>五</div>
                <div>六</div>
                <div>日</div>
            </div>
            <div class='calendar-wrap-left-list4 animated fadeInDown'>
                
            </div>
            <div class='calendar-wrap-left-list5'>
                <div style='display:flex;align-items:center;width:100%;margin-top:12px'>
                    <div style='width: 13px;height: 13px;background: #B2DA78;border-radius: 50%;margin-right:22px'></div>
                    <div class='aside-calendar-blur'>单课</div>
                </div>
                <div style='display:flex;align-items:center;width:100%;margin-top:12px'>
                    <div style='width: 13px;height: 13px;background: #D7D7A0;border-radius: 50%;margin-right:22px'></div>
                    <div class='aside-calendar-blur'>第一节课</div>
                </div>
                <div style='display:flex;align-items:center;width:100%;margin-top:12px'>
                    <div style='width: 13px;height: 13px;background: #DE8386;border-radius: 50%;margin-right:22px'></div>
                    <div class='aside-calendar-blur'>每周课</div>
                </div>
                <div style='display:flex;align-items:center;width:100%;margin-top:12px'>
                    <div style='width: 13px;height: 13px;background: #C3C3C3;border-radius: 50%;margin-right:22px'></div>
                    <div class='aside-calendar-blur'>没时间</div>
                </div>
            </div>
            <div class='calendar-wrap-left-list6'>
                <svg style="width: 25px;height:25px;"  aria-hidden="true">
                    <use xlink:href="#icon-dengluguge"></use>
                </svg>
                <span class='aside-calendar-blur' style='margin-left: 18px;'>Google Calendar</span>
            </div>
        </div> `
    return left
}

let createDate = (params) => {
    $('.calendar-wrap-left-list4')[0].innerHTML = ''
    let { year, month } = params
    dates = new Date(year, month, 0).getDate() //days
    firstday = new Date(`${year}-${month}-1`).getDay() === 0 ? 7 : new Date(`${year}-${month}-1`).getDay() //weekday
    lastMonthDays = firstday - 1
    lastMonthDates = new Date(year, month - 1, 0).getDate() //last month date
    nextMonthDays = 42 - lastMonthDays - dates
    lastMonthDaysArray = []
    currentMonthDaysArray = []
    nextMonthDaysArray = []
    let line = 1

    for (let i = lastMonthDays - 1; i >= 0; i--) {
        lastMonthDaysArray.push(lastMonthDates - i)
    }

    for (let c = 1; c < dates + 1; c++) {
        currentMonthDaysArray.push(c)
    }

    for (let j = 1; j < nextMonthDays + 1; j++) {
        nextMonthDaysArray.push(j)
    }


    let frag = document.createDocumentFragment()
    for (let i = 0; i < lastMonthDaysArray.length; i++) {
        let div = document.createElement('div')
        div.dataset.line = 1
        div.dataset.month = month === 1 ? 12 : month - 1
        div.dataset.year = month === 1 ? year - 1 : year
        div.className = 'calendar-wrap-left-list4-date aside-calendar-blur'
        div.innerHTML = `${lastMonthDaysArray[i]}`
        if (year === new Date().getFullYear()) {
            let index = weekdays.indexOf(lastMonthDaysArray[i])
            if (index != -1) {
                if (currentMonths[index] === month - 1) {
                    div.className = 'calendar-wrap-left-list4-date calendar-week aside-calendar-blur'
                    if (lastMonthDaysArray[i] === weekdays[0]) {
                        div.style.cssText = 'border-radius: 3px 0px 0px 3px;'
                    } else if (lastMonthDaysArray[i] === weekdays[6]) {
                        div.style.cssText = 'border-radius: 0px 3px 3px 0px;'
                    }
                    if (lastMonthDaysArray[i] === new Date().getDate()) {
                        div.className = 'calendar-wrap-left-list4-date calendar-week calendar-today'
                    }
                }
            }
        }
        frag.appendChild(div)
    }

    for (let i = 0; i < currentMonthDaysArray.length; i++) {
        let div = document.createElement('div')
        line = Math.ceil((lastMonthDaysArray.length + currentMonthDaysArray[i]) / 7)
        div.dataset.line = line
        div.dataset.month = month
        div.dataset.year = year
        div.className = 'calendar-wrap-left-list4-date '
        if (year === new Date().getFullYear()) {
            let index = weekdays.indexOf(currentMonthDaysArray[i])
            if (index != -1) {
                if (currentMonths[index] === month) {
                    div.className = 'calendar-wrap-left-list4-date calendar-week'
                    if (currentMonthDaysArray[i] === weekdays[0]) {
                        div.style.cssText = 'border-radius: 3px 0px 0px 3px;'
                    } else if (currentMonthDaysArray[i] === weekdays[6]) {
                        div.style.cssText = 'border-radius: 0px 3px 3px 0px;'
                    }
                    if (currentMonthDaysArray[i] === new Date().getDate()) {
                        div.className = 'calendar-wrap-left-list4-date calendar-week calendar-today'
                    }
                }
            }
        }
        div.innerHTML = `${currentMonthDaysArray[i]}`
        frag.appendChild(div)
    }

    for (let i = 0; i < nextMonthDaysArray.length; i++) {
        let div = document.createElement('div')
        line = Math.ceil((lastMonthDaysArray.length + currentMonthDaysArray.length + nextMonthDaysArray[i]) / 7)
        div.dataset.line = line
        div.dataset.month = month === 12 ? 1 : month + 1
        div.dataset.year = month === 12 ? year + 1 : year
        div.className = 'calendar-wrap-left-list4-date aside-calendar-blur'
        if (year === new Date().getFullYear()) {
            let index = weekdays.indexOf(nextMonthDaysArray[i])
            if (index != -1) {
                if (currentMonths[index] === month + 1) {
                    div.className = 'calendar-wrap-left-list4-date calendar-week aside-calendar-blur'
                    if (nextMonthDaysArray[i] === weekdays[0]) {
                        div.style.cssText = 'border-radius: 3px 0px 0px 3px;'
                    } else if (nextMonthDaysArray[i] === weekdays[6]) {
                        div.style.cssText = 'border-radius: 0px 3px 3px 0px;'
                    }
                    if (nextMonthDaysArray[i] === new Date().getDate()) {
                        div.className = 'calendar-wrap-left-list4-date calendar-week calendar-today'
                    }
                }
            }
        }
        div.innerHTML = `${nextMonthDaysArray[i]}`
        frag.appendChild(div)
    }
    $('.calendar-wrap-left-list4')[0].appendChild(frag)

    let dateListener = () => {
        $('.calendar-wrap-left-list4-date').each(function (i) {
            $(this).on('click', async (e) => {
                let { line } = e.target.dataset
                let month1, month2, year1, year2, years = [], months = []
                weekdays = []
                $('.calendar-wrap-left-list4-date').each(function (index) {
                    if ($(this)[0].dataset.line === line) {
                        $(this)[0].className = 'calendar-wrap-left-list4-date calendar-week'
                        if (index % 7 === 0) {
                            $(this)[0].style.cssText = 'border-radius: 3px 0px 0px 3px;'
                            month1 = $(this)[0].dataset.month
                            year1 = $(this)[0].dataset.year
                        } else if (index % 7 === 6) {
                            $(this)[0].style.cssText = 'border-radius: 0px 3px 3px 0px;'
                            month2 = $(this)[0].dataset.month
                            year2 = $(this)[0].dataset.year
                        }
                        weekdays.push($(this)[0].innerText)
                    } else {
                        $(this)[0].className = 'calendar-wrap-left-list4-date '
                    }
                    years.push($(this)[0].dataset.year)
                    months.push($(this)[0].dataset.month)
                })
                $('.calendar-wrap-right-top')[0].children[0].innerHTML = `${year1}年${month1}月${weekdays[0]}日-${year2}年${month2}月${weekdays[6]}日`
                let weekday = $('.calendar-wrap-right-middle')[0]
                weekday.innerHTML = `
                    <div></div>
                    <div>周一${weekdays[0]}日</div>
                    <div>周二${weekdays[1]}日</div>
                    <div>周三${weekdays[2]}日</div>
                    <div>周四${weekdays[3]}日</div>
                    <div>周五${weekdays[4]}日</div>
                    <div>周六${weekdays[5]}日</div>
                    <div>周日${weekdays[6]}日</div>
                `
                e.target.className = 'calendar-wrap-left-list4-date calendar-week calendar-today'
                let date1 = `${year1}-${month1 < 10 ? 0 + month1 : month1}-${weekdays[0] < 10 ? 0 + weekdays[0] : weekdays[0]} 00:00:00`
                let date2 = `${year2}-${month2 < 10 ? 0 + month2 : month2}-${weekdays[6] < 10 ? 0 + weekdays[6] : weekdays[6]} 00:00:00`
                let res = await getTeacherCourse(date1, date2)
                for (let i = 0; i < 7; i++) {
                    $('.calendar-wrap-right-bottom-list2')[i].innerHTML = ''
                }
                createCalendarRightWeekDay1(res[1], weekdays[0], months[0], years[0])
                createCalendarRightWeekDay2(res[2], weekdays[1], months[1], years[1])
                createCalendarRightWeekDay3(res[3], weekdays[2], months[2], years[2])
                createCalendarRightWeekDay4(res[4], weekdays[3], months[3], years[3])
                createCalendarRightWeekDay5(res[5], weekdays[4], months[4], years[4])
                createCalendarRightWeekDay6(res[6], weekdays[5], months[5], years[5])
                createCalendarRightWeekDay7(res[7], weekdays[6], months[6], years[6])
            })
        })
    }
    dateListener()
}

let lastMonthAllow = () => {
    $('.last-allow')[0].addEventListener('click', (e) => {
        if (month === 1) {
            year -= 1
            month = 12
        } else {
            month -= 1
        }
        weekdays = []
        for (var i = 0; i < 7; i++) {
            weekdays.push(new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * (i - (new Date().getDay() + 6) % 7)).getDate());
        }

        createDate({
            year,
            month
        })
        $('.calendar-wrap-left-list2')[0].children[0].innerText = `${year}年${month}月`
    })
}

let nextMonthAllow = () => {
    $('.right-allow')[0].addEventListener('click', (e) => {
        if (month === 12) {
            year += 1
            month = 1
        } else {
            month += 1
        }
        weekdays = []
        for (var i = 0; i < 7; i++) {
            weekdays.push(new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * (i - (new Date().getDay() + 6) % 7)).getDate());
        }

        createDate({
            year,
            month
        })
        $('.calendar-wrap-left-list2')[0].children[0].innerText = `${year}年${month}月`
    })
}

let createCalendarRight = () => {
    let right = document.createElement('div')
    right.className = 'calendar-wrap-right'
    right.innerHTML = `
        <div class='calendar-wrap-right-top animated bounceInLeft'>
            <span>${year}年${currentMonths[0]}月${weekdays[0]}日-${year}年${currentMonths[6]}月${weekdays[6]}日</span>
            <div class='calendar-sure'>确认</div>
        </div>
        <div class='calendar-wrap-right-middle'>
            <div></div>
            <div>周一${weekdays[0]}日</div>
            <div>周二${weekdays[1]}日</div>
            <div>周三${weekdays[2]}日</div>
            <div>周四${weekdays[3]}日</div>
            <div>周五${weekdays[4]}日</div>
            <div>周六${weekdays[5]}日</div>
            <div>周日${weekdays[6]}日</div>
        </div>
        <div class='calendar-wrap-right-bottom animated fadeInDown'>
            <div class='calendar-wrap-right-bottom-list1' style=''>
                <div>00:00</div>
                <div>01:00</div>
                <div>02:00</div>
                <div>03:00</div>
                <div>04:00</div>
                <div>05:00</div>
                <div>06:00</div>
                <div>07:00</div>
                <div>08:00</div>
                <div>09:00</div>
                <div>10:00</div>
                <div>11:00</div>
                <div>12:00</div>
                <div>13:00</div>
                <div>14:00</div>
                <div>15:00</div>
                <div>16:00</div>
                <div>17:00</div>
                <div>18:00</div>
                <div>19:00</div>
                <div>20:00</div>
                <div>21:00</div>
                <div>22:00</div>
                <div>23:00</div>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
            <div class='calendar-wrap-right-bottom-list2'>
            </div>
        </div>
    `
    return right
}

let createCalendarRightWeekDay1 = (res, day, month, year) => {
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[0].appendChild(frag)
}

let createCalendarRightWeekDay2 = (res, day, month, year) => {
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[1].appendChild(frag)
}

let createCalendarRightWeekDay3 = (res, day, month, year) => {
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[2].appendChild(frag)
}

let createCalendarRightWeekDay4 = (res, day, month, year) => {
    console.log(res)
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.dataset.is_set = true
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[3].appendChild(frag)
}

let createCalendarRightWeekDay5 = (res, day, month, year) => {
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[4].appendChild(frag)
}

let createCalendarRightWeekDay6 = (res, day, month, year) => {
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[5].appendChild(frag)
}

let createCalendarRightWeekDay7 = (res, day, month, year) => {
    let frag = document.createDocumentFragment()
    for (let i = 0; i < 24; i++) {
        let div = document.createElement('div')
        div.className = 'calendar-wrap-right-bottom-list3'
        div.dataset.timequantum = `${i < 10 ? '0' + i : i}:00:00`
        div.dataset.cuday = day
        div.dataset.cumonth = month
        div.dataset.cuyear = year
        if (Object.keys(res[`${i < 10 ? '0' + i : i}:00:00`]).length) {
            let format_date1 = formatDateArray(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet))
            let format_date2 = formatDateArray(new Date(new Date(res[`${i < 10 ? '0' + i : i}:00:00`].lecturet).getTime() + 1000 * 60 * 60))
            let date1 = format_date1.h + ':' + format_date1.minute + ':' + format_date1.s
            let date2 = format_date2.h + ':' + format_date2.minute + ':' + format_date2.s
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has' style='background:#B2DA78'>
                    <div>${date1}-${date2}</div>
                    <div class='calendar-student-name'>${res[`${i < 10 ? '0' + i : i}:00:00`]?.name ? res[`${i < 10 ? '0' + i : i}:00:00`]?.name + '学生' : ''}</div>
                </div>
                <div class='calendar-wrap-right-bottom-list3-bottom' style='background:#B2DA78'></div>
            `
        } else {
            div.innerHTML = `
                <div class='calendar-wrap-right-bottom-list3-top'></div>
                <div class='calendar-wrap-right-bottom-list3-bottom'></div>
            `
        }
        frag.appendChild(div)
    }
    $('.calendar-wrap-right-bottom-list2')[6].appendChild(frag)
}

let planNewClassListener = () => {
    $('.plan-new-class')[0].addEventListener('click', (e) => {
        $('.calendar-sure').show()
        $('.calendar-sure')[0].style.display = 'flex'
        $('.calendar-wrap-right-bottom-list3').each(function (index) {
            $(this)[0].className = 'calendar-wrap-right-bottom-list3 active-edit-class'
            $(this)[0].style.borderLeft = '1px solid #6EA3F9'
            $(this)[0].style.borderBottom = '1px solid #6EA3F9'
        })
        is_edit = true
        setNewCourse()
    })

    $('.set-new-class')[0].addEventListener('click', (e) => {
        $('.calendar-sure').show()
        $('.calendar-sure')[0].style.display = 'flex'
        $('.calendar-wrap-right-bottom-list3').each(function (index) {
            $(this)[0].className = 'calendar-wrap-right-bottom-list3 active-set-class'
            $(this)[0].style.borderLeft = '1px solid #6EA3F9'
            $(this)[0].style.borderBottom = '1px solid #6EA3F9'
        })
        is_edit = true
        setNewCourse()
    })



    /*
    $('.add-new-class')[0].addEventListener('click', (e) => {
        $('.calendar-sure').show()
        $('.calendar-sure')[0].style.display = 'flex'
        $('.calendar-wrap-right-bottom-list3').each(function (index) {
            $(this)[0].className = 'calendar-wrap-right-bottom-list3 active-add-class'
            $(this)[0].style.borderLeft = '1px solid #6EA3F9'
            $(this)[0].style.borderBottom = '1px solid #6EA3F9'
        })
    })*/
}

let setNewCourse = () => {
    if (!$._data($('.calendar-sure')[0], 'events')) {
        $('.calendar-sure')[0].addEventListener('click', async () => {
            if (is_click) return
            is_click = true
            $('.calendar-wrap-right-bottom-list3').each(function (index) {
                $(this)[0].className = 'calendar-wrap-right-bottom-list3'
                $(this)[0].style.borderLeft = '1px solid #C7C7C7'
                $(this)[0].style.borderBottom = '1px solid #C7C7C7'
            })
            let res = await ajax('teaching_time', {
                timequantum2: click
            }, 'post')
            if (res?.code === 200) {
                Dreamer.success('success')
            } else {
                Dreamer.error('fail')
            }
            console.log(res)
            $('.calendar-sure').hide()
            is_edit = false
            is_click = false
            click = []
        })
    }
    Array.from($('.calendar-wrap-right-bottom-list3')).forEach(item => {
        item.addEventListener('click', (e) => {
            if (!is_edit) return
            let { timequantum, cuday, cumonth, cuyear } = $(e.target).parents('div.calendar-wrap-right-bottom-list3')[0].dataset
            let params = {
                lecturet: `${cuyear}-${cumonth}-${cuday} ${timequantum}`,
                zone: Intl.DateTimeFormat().resolvedOptions().timeZone,
                coursetime: new Date(`${cuyear}-${cumonth}-${cuday}`).getDay() === 0 ? '7' : new Date(`${cuyear}-${cumonth}-${cuday}`).getDay(),
                cuyear,
                cumonth,
                cuday,
                userid: JSON.parse(localStorage.getItem('user')).user_id,
                timequantum,
                createtime: `${formatDateArray(new Date()).y}-${formatDateArray(new Date()).month}-${formatDateArray(new Date()).d} ${formatDateArray(new Date()).h}:${formatDateArray(new Date()).minute}:${formatDateArray(new Date()).s}`,
                state: 1
            }
            click.push(params)
            let date1 = new Date(`${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} ${timequantum}`).getTime() + 1000 * 60 * 60
            let date2 = formatDateArray(new Date(date1)).h + ':' + formatDateArray(new Date(date1)).minute + ':' + formatDateArray(new Date(date1)).s
            $(e.target).parents('div.calendar-wrap-right-bottom-list3')[0].children[0].className = 'calendar-wrap-right-bottom-list3-top calendar-wrap-right-bottom-list3-has'
            $(e.target).parents('div.calendar-wrap-right-bottom-list3')[0].children[0].style.background = '#B2DA78'
            $(e.target).parents('div.calendar-wrap-right-bottom-list3')[0].children[0].innerHTML = `
                <div>${timequantum}-${date2}</div>
            `
            $(e.target).parents('div.calendar-wrap-right-bottom-list3')[0].children[1].style.background = '#B2DA78'
            console.log($(e.target).parents('div.calendar-wrap-right-bottom-list3')[0])
        })
    })
}

let searchStudentListener = () => {
    $('.search-student')[0].addEventListener('keyup', async (e) => {
        if (e.keyCode == "13") {
            $('.calendar-student-name').each(function () {
                if ($(this)[0].innerText.includes(e.target.value) && e.target.value !== '') {
                    $(this)[0].style.color = 'red'
                } else {
                    $(this)[0].style.color = 'white'
                }
            })
            console.log(e.target.value)
        }
    })
}